package com.dhcc.sdc.busi.service.impl;

import java.util.*;

import com.dhcc.sdc.base.dao.SdcBaseAssessmentMapper;
import com.dhcc.sdc.base.entity.SdcBaseAssessmentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.busi.dao.SdcBusiEvaluationMapper;
import com.dhcc.sdc.busi.service.ISdcBusiEvaluationService;
import com.dhcc.sdc.busi.entity.SdcBusiEvaluationEntity;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 表数据服务层接口
 * 
 * @ClassName: SdcBusiEvaluationServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-09-14 16:36:24
 */
@Service("sdcBusiEvaluationService")
public class SdcBusiEvaluationServiceImpl extends BaseServiceImpl<SdcBusiEvaluationMapper, SdcBusiEvaluationEntity> implements ISdcBusiEvaluationService {

	@Resource
	private SdcBaseAssessmentMapper sdcBaseAssessmentMapper;

	@Override
	public List<SdcBusiEvaluationEntity> page(Page<SdcBusiEvaluationEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdcBusiEvaluationEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public Integer selectAssessmentQuantity(Long episodeId, String param) {
		Integer res = 0;
		if (StringUtils.isEmpty(param)){
			return res;
		}else {
			String[] split = param.split(",");
			List<String> list = Arrays.asList(split);
			List<Long> assessmentIds = sdcBaseAssessmentMapper.selectByCodes(list);
			if (assessmentIds != null && assessmentIds.size() > 0){
				List<SdcBusiEvaluationEntity> sdcBusiEvaluationEntities = this.baseMapper.selectByEpisodeIdAndAssessmentIds(episodeId, assessmentIds);
				if (sdcBusiEvaluationEntities != null && sdcBusiEvaluationEntities.size() > 0){
					res = sdcBusiEvaluationEntities.size();
				}
			}
		}
		return res;
	}

	@Override
	public Float selectAssessmentScore(Long episodeId, String param) {
		float res = 0;
		if (StringUtils.isEmpty(param)){
			return res;
		}else {
			List<String> list = new ArrayList<>();
			list.add(param);
			List<Long> assessmentIds = sdcBaseAssessmentMapper.selectByCodes(list);
			List<SdcBusiEvaluationEntity> sdcBusiEvaluationEntities = this.baseMapper.selectByEpisodeIdAndAssessmentIds(episodeId, assessmentIds);
			if (sdcBusiEvaluationEntities != null && sdcBusiEvaluationEntities.size() > 0){
				//取最新一条数据
				res = sdcBusiEvaluationEntities.get(0).getScore();
			}

		}
		return res;
	}

	@Override
	public String selectAssessmentGrade(Long episodeId, String param) {
		String res = "";
		if (StringUtils.isEmpty(param)){
			return res;
		}else {
			List<String> list = new ArrayList<>();
			list.add(param);
			List<Long> assessmentIds = sdcBaseAssessmentMapper.selectByCodes(list);
			List<SdcBusiEvaluationEntity> sdcBusiEvaluationEntities = this.baseMapper.selectByEpisodeIdAndAssessmentIds(episodeId, assessmentIds);
			if (sdcBusiEvaluationEntities != null && sdcBusiEvaluationEntities.size() > 0){
				//取最新一条数据
				res = sdcBusiEvaluationEntities.get(0).getLevel();
			}
		}
		return res;
	}

	@Override
	public String selectAssessmentTime(Long episodeId, String param) {
		String res = "";
		if (StringUtils.isEmpty(param)){
			return res;
		}else {
			List<String> list = new ArrayList<>();
			list.add(param);
			List<Long> assessmentIds = sdcBaseAssessmentMapper.selectByCodes(list);
			List<SdcBusiEvaluationEntity> sdcBusiEvaluationEntities = this.baseMapper.selectByEpisodeIdAndAssessmentIds(episodeId, assessmentIds);
			if (sdcBusiEvaluationEntities != null && sdcBusiEvaluationEntities.size() > 0){
				//取最新一条数据
				Date createDate = sdcBusiEvaluationEntities.get(0).getCreateDate();
				if (!StringUtils.isEmpty(createDate)){
					res = com.dhcc.core.framework.util.DateUtil.getTime(createDate);
				}
			}
		}
		return res;
	}

	@Override
	public String selectAssessmentLevelStr(Long episodeId, String param) {
		String res = "";
		if (StringUtils.isEmpty(param)){
			return res;
		}else {
			List<String> list = new ArrayList<>();
			list.add(param);
			List<Long> assessmentIds = sdcBaseAssessmentMapper.selectByCodes(list);
			List<SdcBusiEvaluationEntity> sdcBusiEvaluationEntities = this.baseMapper.selectByEpisodeIdAndAssessmentIds(episodeId, assessmentIds);
			if (sdcBusiEvaluationEntities != null && sdcBusiEvaluationEntities.size() > 0){
				//拼接level的值
				for (SdcBusiEvaluationEntity sdcBusiEvaluationEntity : sdcBusiEvaluationEntities){
					if (!StringUtils.isEmpty(sdcBusiEvaluationEntity.getLevel())){
						res = res + sdcBusiEvaluationEntity.getLevel() + ",";
					}
				}
			}
		}
		return res;
	}

	@Override
	public Integer selectAssessmentQuantityNew(Long episodeId, String dicEntityName, String sqlStr) {
		Integer res = 0;
		if (StringUtils.isEmpty(dicEntityName)){
			return res;
		}else {
			List<Long> assessmentIds = this.getaAssessmentIds(dicEntityName);
			List<SdcBusiEvaluationEntity> sdcBusiEvaluationEntities = this.baseMapper.selectByEpisodeIdAndAssessmentIds(episodeId, assessmentIds);
			if (sdcBusiEvaluationEntities != null && sdcBusiEvaluationEntities.size() > 0){
				res = sdcBusiEvaluationEntities.size();
			}

		}
		return res;
	}

	@Override
	public String selectAssessmentGradeNew(Long episodeId, String dicEntityName, String sqlStr) {
		String res = "";
		if (StringUtils.isEmpty(dicEntityName)){
			return res;
		}else {
			List<Long> assessmentIds = this.getaAssessmentIds(dicEntityName);
			SdcBusiEvaluationEntity sdcBusiEvaluationEntity = this.baseMapper.selectByAssessmentIdsAndTiming(episodeId, assessmentIds, sqlStr);
			if (null != sdcBusiEvaluationEntity){
				res = sdcBusiEvaluationEntity.getLevel();
			}
		}
		return res;
	}

	@Override
	public Float selectAssessmentScoreNew(Long episodeId, String dicEntityName, String sqlStr) {
		Float res = null;
		if (StringUtils.isEmpty(dicEntityName)){
			return res;
		}else {
			List<Long> assessmentIds = this.getaAssessmentIds(dicEntityName);
			SdcBusiEvaluationEntity sdcBusiEvaluationEntity = this.baseMapper.selectByAssessmentIdsAndTiming(episodeId, assessmentIds, sqlStr);
			if (null != sdcBusiEvaluationEntity ){
				res = sdcBusiEvaluationEntity.getScore();
			}

		}
		return res;
	}

	@Override
	public String selectAssessmentTimeNew(Long episodeId, String dicEntityName, String sqlStr) {
		String res = "";
		if (StringUtils.isEmpty(dicEntityName)){
			return res;
		}else {
			List<Long> assessmentIds = this.getaAssessmentIds(dicEntityName);
			SdcBusiEvaluationEntity sdcBusiEvaluationEntity = this.baseMapper.selectByAssessmentIdsAndTiming(episodeId, assessmentIds, sqlStr);
			if (null != sdcBusiEvaluationEntity){
				Date createDate = sdcBusiEvaluationEntity.getCreateDate();
				if (!StringUtils.isEmpty(createDate)){
					res = com.dhcc.core.framework.util.DateUtil.getTime(createDate);
				}
			}
		}
		return res;
	}


	private List<Long> getaAssessmentIds(String dicEntityName){
		List<Long> assessmentIds = new ArrayList<>();
		SdcBaseAssessmentEntity sdcBaseAssessmentEntity = new SdcBaseAssessmentEntity();
		sdcBaseAssessmentEntity.setDescription(dicEntityName);
		SdcBaseAssessmentEntity sdcBaseAssessmentEntity1 = sdcBaseAssessmentMapper.selectOne(sdcBaseAssessmentEntity);
		if (!StringUtils.isEmpty(sdcBaseAssessmentEntity1)){
			assessmentIds = Arrays.asList(sdcBaseAssessmentEntity1.getId());
		}
		return assessmentIds;
	}


}